//格式化时间
//dateTime：时间戳
function getFormtTime(dateTime,flag) {
    if(dateTime != null ) {
        //若传入的dateTime为字符串类型，需要进行转换成数值，若不是无需下面注释代码
        var time = parseInt(dateTime)
        var date = new Date(time);
        //获取年份
        var YY = date.getFullYear();
        //获取月份
        var MM = (date.getMonth() + 1 < 10 ? '0'+(date.getMonth() + 1) : date.getMonth() + 1);
        //获取日期
        var DD = (date.getDate() < 10 ? '0'+date.getDate() : date.getDate());
        if(flag) { //flag为true，显示时分秒格式
            //获取小时
            var hh = (date.getHours() < 10 ? '0'+date.getHours() : date.getHours());
            //获取分
            var mm = (date.getMinutes() < 10 ? '0'+date.getMinutes() : date.getMinutes());
            ///获取秒
            var ss = (date.getSeconds() < 10 ? '0'+date.getSeconds() : date.getSeconds());
            //返回时间格式： 2020-11-09 13:14:52
            return YY + '-' + MM + '-' + DD + ' ' + hh + ':' + mm + ':' + ss;
        } else {
            //返回时间格式： 2020-11-09
            return YY + '-' + MM + '-' + DD;
        }
    } else {
        return "";
    }
}
$.ajax({
    type:"post",
    url:"/market/queryAllMarket",
    data:{},
    dataType:"json",
    success:function(r) {
        if (r.data != null) {
            var t = "<tr>\n" +
                "          <th>序号</th>\n" +
                "          <th>客户姓名</th>\n" +
                "          <th>性别</th>\n" +
                "          <th>联系电话</th>\n" +
                "          <th>所属公司</th>\n" +
                "          <th>所属区域</th>\n" +
                "          <th>状态</th>\n" +
                "          <th>创建时间</th>\n" +
                "          <th>创建人</th>\n" +
                "          <th>分配时间</th>\n" +
                "          <th>客户人员</th>\n" +
                "          <th>操作</th>\n" +
                "        </tr>";
            for (var i = 0; i < r.data.length; i++) {
                var n=""
                var m=""
                var k=""
                if (r.data[i].status=="可用"){
                    n="可用"
                    m="撤销"
                    k="是否确认撤销此条信息？"
                }else {
                    n="不可用"
                    m="恢复"
                    k="是否确认恢复此条信息？"
                }
                t += "<tr> <td>" + r.data[i].customid + "</td><td>" + r.data[i].customname + "</td><td>" + r.data[i].sex + "</td><td>" +r.data[i].telephone+ "</td><td>"+r.data[i].company+"</td><td>"+r.data[i].provin.p_name+"</td><td>"+r.data[i].status+"</td><td>"+getFormtTime(r.data[i].createtime,false)+"</td><td>"+r.data[i].users1.uname+"</td><td>"+getFormtTime(r.data[i].distractime,false)+"</td><td>"+r.data[i].users2.uname+"</td><td>\n" +
                    "          \t<a href=\"javascript:void(0)\" class=\"tablelink\">查看详情</a>\n" +
                    "            <a href=\"javascript:void(0)\" class=\"tablelink\">修改</a>\n" +
                    "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen('"+k+"')\">"+m+"</a>\n" +
                    "            <a href=\"javascript:void(0);\" class=\"tablelink\" onclick=\"allottipOpen()\">分配</a>\n" +
                    "          </td></tr>"
            }
            $(".tablelist").html(t)
            // var pagenum = document.getElementById("pagenum");
            $("#pagenum").html(r.pageNum)
            // var totalrows = document.getElementById("totalrows");
            // totalrows.innerHTML=r.totalRows
            $("#totalrows").html(r.totalRows)

            $(".tablelink").click(function(){

                var text = $(this).text();
                var customid = $(this).parents("tr")[0].children[0].innerHTML
                if (text=="查看详情"){
                    window.location.href="/market/queryOneCustom/"+ customid
                }else if(text=="修改"){
                    window.location.href="/market/updateMarketCustom/"+ customid
                }else if(text=="恢复"){
                    $("#sureMarket").click(function () {
                        $.ajax({
                            type: "post",
                            url: "/market/updateMarketCustomByState/"+ customid+"/"+"可用",
                            data: {},
                            dataType: "json",
                            success: function (r) {
                                if (r=="true"){
                                    alert("修改成功")
                                    location.reload();
                                }else{
                                    alert("修改失败")
                                }
                            }
                        })
                    })
                }else if(text=="撤销"){
                    $("#sureMarket").click(function () {
                        $.ajax({
                            type: "post",
                            url: "/market/updateMarketCustomByState/"+ customid+"/"+"不可用",
                            data: {},
                            dataType: "json",
                            success: function (r) {
                                if (r=="true"){
                                    alert("修改成功")
                                    location.reload();
                                }else{
                                    alert("修改失败")
                                }
                            }
                        })
                    })
                }else if(text=="分配"){
                    $.ajax({
                        type: "post",
                        url: "/market/fenPei/"+ customid,
                        data: {},
                        dataType: "json",
                        success: function (y) {
                            if(y!="false"){
                                 var q=""
                                for (var i=0;i<y.length;i++){
                                    q+= "<option  value='"+y[i].job_id+"'>"+y[i].job_name+"</option>"
                                }
                                $("#jobName").html(q)
                                $("#jobName").blur(function () {
                                    var jobId=$("#jobName option:selected")
                                    var jobId1=jobId.val()
                                    $.ajax({
                                        type: "post",
                                        url: "/market/queryUserByJob",
                                        data: {job_id:jobId1},
                                        dataType: "json",
                                        success: function (x) {
                                            if (x!="false"){
                                                var n=""
                                                for (var i=0;i<x.length;i++){
                                                    n += "<option value='"+x[i].uId+"'>"+x[i].uname+"</option>"
                                                }
                                                $("#userName").html(n)
                                                $("#fenpei").click(function () {
                                                    var userId=$("#userName option:selected")
                                                    var userId1=userId.val()
                                                    $.ajax({
                                                        type: "post",
                                                        url: "/market/saveFenPei",
                                                        data: {uId: userId1},
                                                        dataType: "json",
                                                        success: function (m) {
                                                            if(m=="true"){
                                                                alert("分配成功！")
                                                                location.reload();
                                                            }else {
                                                                alert("分配失败!")
                                                            }
                                                        }
                                                    })
                                                })
                                            }
                                        }
                                    })
                                })
                            }else {
                                alert("当前部门还没有职位!")
                            }
                        }
                    })
                }
            });


            $("#shangyiye").click(function () {
                if (pagenum.innerHTML>1){
                    pagenum.innerHTML=Number(pagenum.innerHTML)-1;
                }else{
                    alert("已经是第一页了")
                }

                var n=pagenum.innerHTML
                if (n>=1){
                    $.ajax({
                        type:"post",
                        url:"/market/queryAllMarket",
                        data:{pageNum:n},
                        dataType:"json",
                        success:function(r){
                            if (r.data != null){
                                var t = "<tr>\n" +
                                    "          <th>序号</th>\n" +
                                    "          <th>客户姓名</th>\n" +
                                    "          <th>性别</th>\n" +
                                    "          <th>联系电话</th>\n" +
                                    "          <th>所属公司</th>\n" +
                                    "          <th>所属区域</th>\n" +
                                    "          <th>状态</th>\n" +
                                    "          <th>创建时间</th>\n" +
                                    "          <th>创建人</th>\n" +
                                    "          <th>分配时间</th>\n" +
                                    "          <th>客户人员</th>\n" +
                                    "          <th>操作</th>\n" +
                                    "        </tr>";
                                for (var i = 0; i < r.data.length; i++) {
                                    var n=""
                                    var m=""
                                    var k=""
                                    if (r.data[i].status=="可用"){
                                        n="可用"
                                        m="撤销"
                                        k="是否确认撤销此条信息？"
                                    }else {
                                        n="不可用"
                                        m="恢复"
                                        k="是否确认恢复此条信息？"
                                    }
                                    t += "<tr> <td>" + r.data[i].customid + "</td><td>" + r.data[i].customname + "</td><td>" + r.data[i].sex + "</td><td>" +r.data[i].telephone+ "</td><td>"+r.data[i].company+"</td><td>"+r.data[i].provin.p_name+"</td><td>"+r.data[i].status+"</td><td>"+getFormtTime(r.data[i].createtime,false)+"</td><td>"+r.data[i].users1.uname+"</td><td>"+getFormtTime(r.data[i].distractime,false)+"</td><td>"+r.data[i].users2.uname+"</td><td>\n" +
                                        "          \t<a href=\"javascript:void(0)\" class=\"tablelink\">查看详情</a>\n" +
                                        "            <a href=\"javascript:void(0)\" class=\"tablelink\">修改</a>\n" +
                                        "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen('"+k+"')\">"+m+"</a>\n" +
                                        "            <a href=\"javascript:void(0);\" class=\"tablelink\" onclick=\"allottipOpen()\">分配</a>\n" +
                                        "          </td></tr>"
                                }
                                $(".tablelist").html(t)

                                $(".tablelink").click(function(){

                                    var text = $(this).text();
                                    var customid = $(this).parents("tr")[0].children[0].innerHTML
                                    if (text=="查看详情"){
                                        window.location.href="/market/queryOneCustom/"+ customid
                                    }else if(text=="修改"){
                                        window.location.href="/market/updateMarketCustom/"+ customid
                                    }else if(text=="恢复"){
                                        $("#sureMarket").click(function () {
                                            $.ajax({
                                                type: "post",
                                                url: "/market/updateMarketCustomByState/"+ customid+"/"+"可用",
                                                data: {},
                                                dataType: "json",
                                                success: function (r) {
                                                    if (r=="true"){
                                                        alert("修改成功")
                                                        location.reload();
                                                    }else{
                                                        alert("修改失败")
                                                    }
                                                }
                                            })
                                        })
                                    }else if(text=="撤销"){
                                        $("#sureMarket").click(function () {
                                            $.ajax({
                                                type: "post",
                                                url: "/market/updateMarketCustomByState/"+ customid+"/"+"不可用",
                                                data: {},
                                                dataType: "json",
                                                success: function (r) {
                                                    if (r=="true"){
                                                        alert("修改成功")
                                                        location.reload();
                                                    }else{
                                                        alert("修改失败")
                                                    }
                                                }
                                            })
                                        })
                                    }else if (text=="分配"){
                                        $.ajax({
                                            type: "post",
                                            url: "/market/fenPei/"+ customid,
                                            data: {},
                                            dataType: "json",
                                            success: function (y) {
                                                if(y!="false"){
                                                    var q=""
                                                    for (var i=0;i<y.length;i++){
                                                        q+= "<option  value='"+y[i].job_id+"'>"+y[i].job_name+"</option>"
                                                    }
                                                    $("#jobName").html(q)
                                                    $("#jobName").blur(function () {
                                                        var jobId=$("#jobName option:selected")
                                                        var jobId1=jobId.val()
                                                        $.ajax({
                                                            type: "post",
                                                            url: "/market/queryUserByJob",
                                                            data: {job_id:jobId1},
                                                            dataType: "json",
                                                            success: function (x) {
                                                                if (x!="false"){
                                                                    var n=""
                                                                    for (var i=0;i<x.length;i++){
                                                                        n += "<option value='"+x[i].uId+"'>"+x[i].uname+"</option>"
                                                                    }
                                                                    $("#userName").html(n)
                                                                    $("#fenpei").click(function () {
                                                                        var userId=$("#userName option:selected")
                                                                        var userId1=userId.val()
                                                                        $.ajax({
                                                                            type: "post",
                                                                            url: "/market/saveFenPei",
                                                                            data: {uId: userId1},
                                                                            dataType: "json",
                                                                            success: function (m) {
                                                                                if(m=="true"){
                                                                                    alert("分配成功！")
                                                                                    location.reload();
                                                                                }else {
                                                                                    alert("分配失败!")
                                                                                }
                                                                            }
                                                                        })
                                                                    })
                                                                }
                                                            }
                                                        })
                                                    })
                                                }else {
                                                    alert("当前部门还没有职位!")
                                                }
                                            }
                                        })
                                    }
                                });

                            }
                        },
                        error:function (r) {
                            alert("!!!")
                        }

                    })
                }else{
                    alert("已经是第一页了")
                }


            })


            $("#xiayiye").click(function () {
                if (pagenum.innerHTML<r.totalPage){
                    pagenum.innerHTML=Number(pagenum.innerHTML)+1
                }else{
                    alert("已经是最后一页了")
                }

                var n=pagenum.innerHTML
                if (n<=r.totalPage){
                    $.ajax({
                        type:"post",
                        url:"/market/queryAllMarket",
                        data:{pageNum:n},
                        dataType:"json",
                        success:function(r){
                            if (r.data != null){
                                var t = "<tr>\n" +
                                    "          <th>序号</th>\n" +
                                    "          <th>客户姓名</th>\n" +
                                    "          <th>性别</th>\n" +
                                    "          <th>联系电话</th>\n" +
                                    "          <th>所属公司</th>\n" +
                                    "          <th>所属区域</th>\n" +
                                    "          <th>状态</th>\n" +
                                    "          <th>创建时间</th>\n" +
                                    "          <th>创建人</th>\n" +
                                    "          <th>分配时间</th>\n" +
                                    "          <th>客户人员</th>\n" +
                                    "          <th>操作</th>\n" +
                                    "        </tr>";
                                for (var i = 0; i < r.data.length; i++) {
                                    var n=""
                                    var m=""
                                    var k=""
                                    if (r.data[i].status=="可用"){
                                        n="可用"
                                        m="撤销"
                                        k="是否确认撤销此条信息？"
                                    }else {
                                        n="不可用"
                                        m="恢复"
                                        k="是否确认恢复此条信息？"
                                    }
                                    t += "<tr> <td>" + r.data[i].customid + "</td><td>" + r.data[i].customname + "</td><td>" + r.data[i].sex + "</td><td>" +r.data[i].telephone+ "</td><td>"+r.data[i].company+"</td><td>"+r.data[i].provin.p_name+"</td><td>"+r.data[i].status+"</td><td>"+getFormtTime(r.data[i].createtime,false)+"</td><td>"+r.data[i].users1.uname+"</td><td>"+getFormtTime(r.data[i].distractime,false)+"</td><td>"+r.data[i].users2.uname+"</td><td>\n" +
                                        "          \t<a href=\"javascript:void(0)\" class=\"tablelink\">查看详情</a>\n" +
                                        "            <a href=\"javascript:void(0)\" class=\"tablelink\">修改</a>\n" +
                                        "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen('"+k+"')\">"+m+"</a>\n" +
                                        "            <a href=\"javascript:void(0);\" class=\"tablelink\" onclick=\"allottipOpen()\">分配</a>\n" +
                                        "          </td></tr>"
                                }
                                $(".tablelist").html(t)

                                $(".tablelink").click(function(){

                                    var text = $(this).text();
                                    var customid = $(this).parents("tr")[0].children[0].innerHTML
                                    if (text=="查看详情"){
                                        window.location.href="/market/queryOneCustom/"+ customid
                                    }else if(text=="修改"){
                                        window.location.href="/market/updateMarketCustom/"+ customid
                                    }else if(text=="恢复"){
                                        $("#sureMarket").click(function () {
                                            $.ajax({
                                                type: "post",
                                                url: "/market/updateMarketCustomByState/"+ customid+"/"+"可用",
                                                data: {},
                                                dataType: "json",
                                                success: function (r) {
                                                    if (r=="true"){
                                                        alert("修改成功")
                                                        location.reload();
                                                    }else{
                                                        alert("修改失败")
                                                    }
                                                }
                                            })
                                        })
                                    }else if(text=="撤销"){
                                        $("#sureMarket").click(function () {
                                            $.ajax({
                                                type: "post",
                                                url: "/market/updateMarketCustomByState/"+ customid+"/"+"不可用",
                                                data: {},
                                                dataType: "json",
                                                success: function (r) {
                                                    if (r=="true"){
                                                        alert("修改成功")
                                                        location.reload();
                                                    }else{
                                                        alert("修改失败")
                                                    }
                                                }
                                            })
                                        })
                                    }else if (text=="分配"){
                                        $.ajax({
                                            type: "post",
                                            url: "/market/fenPei/"+ customid,
                                            data: {},
                                            dataType: "json",
                                            success: function (y) {
                                                if(y!="false"){
                                                    var q=""
                                                    for (var i=0;i<y.length;i++){
                                                        q+= "<option  value='"+y[i].job_id+"'>"+y[i].job_name+"</option>"
                                                    }
                                                    $("#jobName").html(q)
                                                    $("#jobName").blur(function () {
                                                        var jobId=$("#jobName option:selected")
                                                        var jobId1=jobId.val()
                                                        $.ajax({
                                                            type: "post",
                                                            url: "/market/queryUserByJob",
                                                            data: {job_id:jobId1},
                                                            dataType: "json",
                                                            success: function (x) {
                                                                if (x!="false"){
                                                                    var n=""
                                                                    for (var i=0;i<x.length;i++){
                                                                        n += "<option value='"+x[i].uId+"'>"+x[i].uname+"</option>"
                                                                    }
                                                                    $("#userName").html(n)
                                                                    $("#fenpei").click(function () {
                                                                        var userId=$("#userName option:selected")
                                                                        var userId1=userId.val()
                                                                        $.ajax({
                                                                            type: "post",
                                                                            url: "/market/saveFenPei",
                                                                            data: {uId: userId1},
                                                                            dataType: "json",
                                                                            success: function (m) {
                                                                                if(m=="true"){
                                                                                    alert("分配成功！")
                                                                                    location.reload();
                                                                                }else {
                                                                                    alert("分配失败!")
                                                                                }
                                                                            }
                                                                        })
                                                                    })
                                                                }
                                                            }
                                                        })
                                                    })
                                                }else {
                                                    alert("当前部门还没有职位!")
                                                }
                                            }
                                        })
                                    }
                                });
                            }
                        },
                        error:function (r) {
                            alert("!!!")
                        }

                    })
                }else{
                    alert("已经是最后一页了")
                }


            })


        }
    }
})

$("#check").click(function () {
    var companyName = $("#companyName").val();
    var customName = $("#customName").val();
    var province = $("#province option:selected").text();
    var address1;
    if (province=="江西"){
        address1="1"
    }else if(province=="广东"){
        address1="2"
    }else if(province=="江苏"){
        address1="3"
    }else if(province=="安徽"){
        address1="4"
    }else if(province=="浙江"){
        address1="5"
    }
    var status = $("#status option:selected").text();
    $.ajax({
        type: "post",
        url: "/market/queryAllCustomByCondition",
        data: {company:companyName,customname:customName,address:address1,status:status},
        dataType: "json",
        success: function (r) {
            if(r!="null"){
                var t = "<tr>\n" +
                    "          <th>序号</th>\n" +
                    "          <th>客户姓名</th>\n" +
                    "          <th>性别</th>\n" +
                    "          <th>联系电话</th>\n" +
                    "          <th>所属公司</th>\n" +
                    "          <th>所属区域</th>\n" +
                    "          <th>状态</th>\n" +
                    "          <th>创建时间</th>\n" +
                    "          <th>创建人</th>\n" +
                    "          <th>分配时间</th>\n" +
                    "          <th>客户人员</th>\n" +
                    "          <th>操作</th>\n" +
                    "        </tr>";
                for (var i = 0; i < r.length; i++) {
                    var n=""
                    var m=""
                    var k=""
                    if (r[i].status=="可用"){
                        n="可用"
                        m="撤销"
                        k="是否确认撤销此条信息？"
                    }else {
                        n="不可用"
                        m="恢复"
                        k="是否确认恢复此条信息？"
                    }
                    t += "<tr> <td>" + r[i].customid + "</td><td>" + r[i].customname + "</td><td>" + r[i].sex + "</td><td>" +r[i].telephone+ "</td><td>"+r[i].company+"</td><td>"+r[i].provin.p_name+"</td><td>"+r[i].status+"</td><td>"+getFormtTime(r[i].createtime,false)+"</td><td>"+r[i].users1.uname+"</td><td>"+getFormtTime(r[i].distractime,false)+"</td><td>"+r[i].users2.uname+"</td><td>\n" +
                        "          \t<a href=\"javascript:void(0)\" class=\"tablelink\">查看详情</a>\n" +
                        "            <a href=\"javascript:void(0)\" class=\"tablelink\">修改</a>\n" +
                        "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen('"+k+"')\">"+m+"</a>\n" +
                        "            <a href=\"javascript:void(0);\" class=\"tablelink\" onclick=\"allottipOpen()\">分配</a>\n" +
                        "          </td></tr>"
                }
                $(".tablelist").html(t)

                $(".tablelink").click(function(){

                    var text = $(this).text();
                    var customid = $(this).parents("tr")[0].children[0].innerHTML
                    if (text=="查看详情"){
                        window.location.href="/market/queryOneCustom/"+ customid
                    }else if(text=="修改"){
                        window.location.href="/market/updateMarketCustom/"+ customid
                    }else if(text=="恢复"){
                        $("#sureMarket").click(function () {
                            $.ajax({
                                type: "post",
                                url: "/market/updateMarketCustomByState/"+ customid+"/"+"可用",
                                data: {},
                                dataType: "json",
                                success: function (r) {
                                    if (r=="true"){
                                        alert("修改成功")
                                        location.reload();
                                    }else{
                                        alert("修改失败")
                                    }
                                }
                            })
                        })
                    }else if(text=="撤销"){
                        $("#sureMarket").click(function () {
                            $.ajax({
                                type: "post",
                                url: "/market/updateMarketCustomByState/"+ customid+"/"+"不可用",
                                data: {},
                                dataType: "json",
                                success: function (r) {
                                    if (r=="true"){
                                        alert("修改成功")
                                        location.reload();
                                    }else{
                                        alert("修改失败")
                                    }
                                }
                            })
                        })
                    }else if (text=="分配"){
                        $.ajax({
                            type: "post",
                            url: "/market/fenPei/"+ customid,
                            data: {},
                            dataType: "json",
                            success: function (y) {
                                if(y!="false"){
                                    var q=""
                                    for (var i=0;i<y.length;i++){
                                        q+= "<option  value='"+y[i].job_id+"'>"+y[i].job_name+"</option>"
                                    }
                                    $("#jobName").html(q)
                                    $("#jobName").blur(function () {
                                        var jobId=$("#jobName option:selected")
                                        var jobId1=jobId.val()
                                        $.ajax({
                                            type: "post",
                                            url: "/market/queryUserByJob",
                                            data: {job_id:jobId1},
                                            dataType: "json",
                                            success: function (x) {
                                                if (x!="false"){
                                                    var n=""
                                                    for (var i=0;i<x.length;i++){
                                                        n += "<option value='"+x[i].uId+"'>"+x[i].uname+"</option>"
                                                    }
                                                    $("#userName").html(n)
                                                    $("#fenpei").click(function () {
                                                        var userId=$("#userName option:selected")
                                                        var userId1=userId.val()
                                                        $.ajax({
                                                            type: "post",
                                                            url: "/market/saveFenPei",
                                                            data: {uId: userId1},
                                                            dataType: "json",
                                                            success: function (m) {
                                                                if(m=="true"){
                                                                    alert("分配成功！")
                                                                    location.reload();
                                                                }else {
                                                                    alert("分配失败!")
                                                                }
                                                            }
                                                        })
                                                    })
                                                }
                                            }
                                        })
                                    })
                                }else {
                                    alert("当前部门还没有职位!")
                                }
                            }
                        })
                    }
                });
            }else {
                alert("没有查询结果！")
                location.href="customerList.html"
            }
        }
    })
})


